//
//  CTPersonalProfileViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/29.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTPersonalProfileViewController.h"
#import "CTPersonalProfileDetailsViewController.h"
#import "CTPickImageViewController.h"

//head
#import "CTHeader.h"

//view
#import "CTPersonalProfileView.h"

//function
#import "CTDebugManager.h"
#import "CTUser.h"
#import "CTAppLanguage.h"
#import "CTTextManager.h"
#import "CTLogin.h"
#import "CTActivitorView.h"
#import "CTRemindView.h"
#import "MobClick.h"

//logic
#import "CTThirdPartyRegisterLogic.h"

//model
#import "CTUserItem.h"


@interface CTPersonalProfileViewController ()<CTPersonalProfileViewDelegate>
{
    CTPersonalProfileView * personalProfileView;
    CTThirdPartyRegisterLogic * logic;
}
@end

@implementation CTPersonalProfileViewController

- (void)dealloc
{
    logic = nil;
    

    
    [personalProfileView removeFromSuperview];
    personalProfileView = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_SET_HEAD object:nil];
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTPersonalProfileViewController Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        logic = [[CTThirdPartyRegisterLogic alloc] init];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setPersonalHead:) name:CT_NOTI_SET_HEAD object:nil];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //统计
    [MobClick beginLogPageView:@"CTPersonalProfileViewController"];
    CTUserItem * userItem = [CTUser userInfo];
 //   personalProfileView.imageStr = userItem.profile_image_url_thumb;
    personalProfileView.signatureStr = userItem.sign;
    personalProfileView.nameStr = userItem.name;
    personalProfileView.userIDStr = userItem.uid;
    personalProfileView.sexStr = userItem.gender;
    personalProfileView.productionStr = userItem.group_count;
    personalProfileView.favStr = userItem.public_count;
    if ([userItem.verified isEqualToString:@"0"])personalProfileView.vipBool =NO;
    else personalProfileView.vipBool = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //统计
    [MobClick endLogPageView:@"CTPersonalProfileViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    personalProfileView = [[CTPersonalProfileView alloc] init];
    personalProfileView.delegate = self;
    [self.view addSubview:personalProfileView];
    
    CTUserItem * userItem = [CTUser userInfo];
    personalProfileView.imageStr = userItem.profile_image_url_thumb;
    personalProfileView.signatureStr = userItem.sign;
    personalProfileView.nameStr = userItem.name;
    personalProfileView.userIDStr = userItem.uid;
    personalProfileView.sexStr = userItem.gender;
    personalProfileView.productionStr = userItem.group_count;
    personalProfileView.favStr = userItem.public_count;
    if ([userItem.verified isEqualToString:@"0"])personalProfileView.vipBool =NO;
    else personalProfileView.vipBool = YES;
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - personalProfile view delegate -
- (void)personalProfileViewHeadBtn:(CTPersonalProfileView *) view
{
    CTPickImageViewController * pickImageVC = [[CTPickImageViewController alloc] init];
    pickImageVC.type = CTPickImageViewControllerTypeSetHead;
    UINavigationController * pickImageNav = [[UINavigationController alloc] initWithRootViewController:pickImageVC];
    
    pickImageNav.navigationBarHidden = YES;
    [self presentViewController:pickImageNav animated:YES completion:^{
        
    }];
    
}
- (void)personalProfileViewUserNameBtn:(CTPersonalProfileView *) view
{
    CTUserItem * userItem = [CTUser userInfo];
    
    CTPersonalProfileDetailsViewController * personalProfileDetailsVC = [[CTPersonalProfileDetailsViewController alloc] init];
    personalProfileDetailsVC.type = CTPersonalProfileDetailsTypeName;
    personalProfileDetailsVC.nameStr = userItem.name;
    [self.navigationController pushViewController:personalProfileDetailsVC animated:YES];
}

- (void)personalProfileViewSexBtn:(CTPersonalProfileView *) view
{
    CTUserItem * userItem = [CTUser userInfo];
    
    CTPersonalProfileDetailsViewController * personalProfileDetailsVC = [[CTPersonalProfileDetailsViewController alloc] init];
    personalProfileDetailsVC.type = CTPersonalProfileDetailsTypeGender;
    personalProfileDetailsVC.genderStr = userItem.gender;
    [self.navigationController pushViewController:personalProfileDetailsVC animated:YES];
}
- (void)personalProfileViewsignatureBtn:(CTPersonalProfileView *) view
{
    CTUserItem * userItem = [CTUser userInfo];
    
    CTPersonalProfileDetailsViewController * personalProfileDetailsVC = [[CTPersonalProfileDetailsViewController alloc] init];
    personalProfileDetailsVC.type = CTPersonalProfileDetailsTypeSign;
    personalProfileDetailsVC.signStr = userItem.sign;
    [self.navigationController pushViewController:personalProfileDetailsVC animated:YES];
}
- (void)personalProfileViewUserIDBtn:(CTPersonalProfileView *)  view
{
    
}

- (void)personalProfileViewBackBtn:(CTPersonalProfileView *)view
{
    [self.navigationController popViewControllerAnimated:YES];
}





#pragma mark - noti -

-(void)setPersonalHead:(id)sender
{
    NSNotification * noti = (NSNotification *)sender;
    UIImage * headImg = (UIImage *)noti.object;
    
    personalProfileView.headerImage = headImg;
    
   
    
    NSString * languageStr = [CTAppLanguage systemLanguage];
    
    CTUserInfoModifyRequestItem * modifyItem = [[CTUserInfoModifyRequestItem alloc] init];
    modifyItem.token = [CTLogin token];
    modifyItem.head = UIImagePNGRepresentation(headImg);
    if ([languageStr isEqualToString:@"zh-Hans"])modifyItem.lang = @"cn";
    else modifyItem.lang = @"en";
    
    
    [logic thirdPartyRegisterRequest:modifyItem successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{            
        });
        
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{

        });
    }];
}


@end
